package com.landinfo.service.impl;

import com.landinfo.domain.ResourcesSurvey;
import com.landinfo.repository.ResourcesSurveyRepository;
import com.landinfo.service.ResourcesSurveyService;
import com.landinfo.service.dto.ResourcesSurveyDTO;
import com.landinfo.service.mapper.ResourcesSurveyMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 资源简介
 */
@Service
@Transactional
public class ResourcesSurveyServiceImpl implements ResourcesSurveyService {

    private final Logger log = LoggerFactory.getLogger(ResourcesSurveyServiceImpl.class);

    private final ResourcesSurveyRepository resourcesSurveyRepository;

    private final ResourcesSurveyMapper resourcesSurveyMapper;

    public ResourcesSurveyServiceImpl(ResourcesSurveyRepository resourcesSurveyRepository, ResourcesSurveyMapper resourcesSurveyMapper) {
        this.resourcesSurveyRepository = resourcesSurveyRepository;
        this.resourcesSurveyMapper = resourcesSurveyMapper;
    }

    @Override
    public ResourcesSurveyDTO save(ResourcesSurveyDTO resourcesSurveyDTO) {
        log.debug("Request to save ResourcesSurvey : {}", resourcesSurveyDTO);
        ResourcesSurvey resourcesSurvey = resourcesSurveyMapper.toEntity(resourcesSurveyDTO);
        resourcesSurvey = resourcesSurveyRepository.save(resourcesSurvey);
        return resourcesSurveyMapper.toDto(resourcesSurvey);
    }

    @Override
    public List<ResourcesSurveyDTO> findAll() {
        List<ResourcesSurveyDTO> result = resourcesSurveyRepository.findAll().stream()
                .map(resourcesSurveyMapper::toDto)
                .collect(Collectors.toCollection(LinkedList::new));
        return result;
    }

    @Override
    public List<Object> getYears() {
        return resourcesSurveyRepository.getYears();
    }

    @Override
    public List<ResourcesSurveyDTO> findByXzqhCode(String xzqhCode, Integer year, String regionType) {
//        // 处理数据用代码
//        insertDatas();

        List<ResourcesSurveyDTO> result = resourcesSurveyRepository.findByXzqhCodeAndYearAndRegionType(xzqhCode,year,regionType).stream()
                .map(resourcesSurveyMapper::toDto)
                .collect(Collectors.toCollection(LinkedList::new));
        return result;
    }

    @Override
    public List<ResourcesSurveyDTO> findByXzqhParentCode(String xzqhCode) {
        List<ResourcesSurveyDTO> result = resourcesSurveyRepository.findByXzqhParentCode(xzqhCode).stream()
                .map(resourcesSurveyMapper::toDto)
                .collect(Collectors.toCollection(LinkedList::new));
        return result;
    }

    @Override
    public ResourcesSurveyDTO findOne(String id) {
        log.debug("Request to get ResourcesSurvey : {}", id);
        ResourcesSurvey resource = resourcesSurveyRepository.findOneById(id);
        return resourcesSurveyMapper.toDto(resource);
    }

    @Override
    public void delete(String id) {
        log.debug("Request to delete ResourcesSurvey : {}", id);
        resourcesSurveyRepository.deleteById(id);
    }

    // 处理数据库资源现状表中父级与子级行政区划未关联上的的问题
    private void insertDatas(){
        List<ResourcesSurvey> list = new LinkedList<>(resourcesSurveyRepository.findAll());
        if(list.size() > 0){
            for (int i=0; i<list.size(); i++){
                ResourcesSurvey temp1 = list.get(i);

                // 先找出子级
                Set<ResourcesSurvey> tempList = new HashSet<>();
                for (int j=0; j<list.size(); j++){
                    ResourcesSurvey temp2 = list.get(j);
                    if(temp2.getXzqhParentCode().equals(temp1.getXzqhCode()) &&
                            temp2.getRegionType().equals(temp1.getRegionType()) &&
                            temp2.getYear().equals(temp1.getYear())){
                        tempList.add(temp2);
                    }
                }
                if(tempList.size() > 0){
                    temp1.setChildren(tempList);
                }
                // 更新
                ResourcesSurvey a = resourcesSurveyRepository.save(temp1);
                log.debug("更新第"+(i+1)+"条");
            }
        }
    }
}
